@page "/logs"

@inject IJSRuntime JsRuntime;
@inject IClientActorSystem ActorSystem;

@using Microsoft.AspNetCore.Components
@using Orleankka
@using Orleankka.Client
@using ProcessManager

<div className="log-container">
    <pre ref="logPanel" className="log">
        @foreach (var each in events) {
            <span>@each</span>
        }    
    </pre>
</div>

@code {
    StreamRef<JobEvent> notifications;
    readonly List<string> events = new List<string>();

    protected override async Task OnInitializedAsync()
    {
        notifications = ActorSystem.StreamOf<JobEvent>("notifications", "copier");
        await notifications.Subscribe<JobEvent>(async e =>
        {
            if (events.Count > 200)
                events.RemoveAt(events.Count - 1);

            events.Add(e.ToString());
            await InvokeAsync(StateHasChanged);            
        });
    }
}
